$(function(){
  var provinceHtml = "",cityHtml = "",districtHtml = "",address = ""
  var province_list,city_list,district_list;
  // 获取地址浏览
  $.ajax({
    type: "get",
    url: "001.json",
    dataType: "json",
    success: function(data){ 
      province_list = data.province_list
      city_list = data.city_list
      district_list = data.district_list
      // 省
      $.each(province_list, function(Index, value){
        provinceHtml += "<a data-province=" + value.province_id + ">"+ value.province_name +"</a>"
        if(value.province_id == 1){
          $("#province_active").html(value.province_name)
        }
      })
      $('#province_list').html(provinceHtml);
      // 市
      $.each(city_list, function(index, value){
        if(value.province_id == 1){
          cityHtml += "<a data-city=" + value.city_id + ">"+ value.city_name +"</a>"
          if(value.city_id == 1){
            $("#city_active").html(value.city_name)
          }
        }
      })
      $('#city_list').html(cityHtml);
      // 县
      $.each(district_list, function(index, value){
        if(value.city_id == 1){
          districtHtml += "<a data-city=" + value.district_id + ">"+ value.district_name +"</a>"
          if(value.district_id == 1){
            $("#district_active").html(value.district_name)
          }
        }
      })
      $('#district_list').html(districtHtml);
    }
  });

  // 触摸显示
  addressHover()
  function addressHover(){
    $("#ptAddress").mouseenter(function(){
      $("#ptAddressContent").show()
      $("#addressTabs p").removeClass("active")
      $(".addressTabs-content").hide()
      $("#province_active").addClass("active")
      $("#province_list").parent().show()
    })
    $("#ptAddress").mouseleave(function(){
      $("#ptAddressContent").hide()
    })
  }
  
// 切换城市
  $("#province_list").on("click","a", function(e){
    $("#ptAddress").off()
    var target = $(event.target)
    var Index= target.attr("data-province")
    cityHtml = ""
    $.each(city_list, function(index, value){
      if(value.province_id == Index){
        cityHtml += "<a data-city=" + value.city_id + ">"+ value.city_name +"</a>"
      }
    })
    $('#city_list').html(cityHtml);
    $("#province_active").removeClass("active").html(target.html())
    $("#province_list").parent().hide()
    $("#city_active").addClass("active")
    $("#city_list").parent().show()
  })
  $("#city_list").on("click","a", function(e){
    var target = $(event.target)
    var Index= target.attr("data-city")
    districtHtml = ""
    $.each(district_list, function(index, value){
      if(value.city_id == Index){
        districtHtml += "<a data-city=" + value.district_id + ">"+ value.district_name +"</a>"
      }
    })
    $('#district_list').html(districtHtml);
    $("#city_active").removeClass("active").html(target.html())
    $("#city_list").parent().hide()
    $("#district_active").addClass("active")
    $("#district_list").parent().show()
  })
  $("#district_list").on("click","a", function(e){
    var target = $(event.target)
    $("#district_active").html(target.html())
    $("#address-first").html($("#province_active").html() + $("#city_active").html() + target.html())
    $("#ptAddressContent").hide()
    setTimeout(function(){
      addressHover()
    }, 100);
  })
  
//放大镜列表滚动
  
  var distance = 70
  var listlength = $("#magnifyList li").length
  var Left = $("#magnifyList").position().left;
  $("#magnifyList").css({"width":(70*listlength-10) + "px"})
  var gap = $("#magnifyList").width() - $("#magnifyList").parent().width()

// 前进
  $("#btnImgnext").click(function(){
    Left = $("#magnifyList").position().left - distance
    if(Left < -gap){
      Left = -gap
    }
    $("#magnifyList").animate({
      "left": Left + "px"
    }, 300)
  })
  // 后退
  $("#btnImgprev").click(function(){
    Left = $("#magnifyList").position().left + distance
    if(Left > 0){
      Left = 0
    }
    $("#magnifyList").animate({
      "left": Left + "px"
    }, 300)
  })


  changgeMagnifyImg("#magnifyList")
// 切换放大镜图片
  function changgeMagnifyImg(list){
    $(list).children("li").hover(function(){
      var Imgsrc = $(this).find("img").attr("src")
      $("#smallImg img").attr("src",Imgsrc)
      $("#bigImg img").attr("src",Imgsrc)
    })
  }

  magnifyimg("#smallImg","#bigImg","#smallImgInfo")
  // 放大镜
  function magnifyimg(small,big,info){
    //鼠标进入图片
    $(small).mouseenter(function(){
      $(big).show()
      $(info).show()
    })
    //鼠标移出图片
    $(small).mouseleave(function(){
      $(big).hide()
      $(info).hide()
    })
    //鼠标移动
    $(small).mousemove(function(e){     
      var X = e.pageX - $(small).offset().left - $(info).width()/2
      var Y = e.pageY - Math.round($(small).offset().top) - $(info).height()/2
      // 移动范围设置
      if(X <= 0){
        X = 0
      }else if(X >= $(info).width()){
        X = $(info).width()
      }
      if(Y <= 0){
        Y = 0
      }else if(Y >= $(info).height()){
        Y = $(info).height()
      }
      $(info).css({
        "left": X + "px",
        "top":Y + "px"
      })
      // 移动比例
      var bili = $(big).width()/$(info).width();
			var bigx = bili*X;
      var bigy = bili*Y;
			$(big).find("img").css({
        "left": -bigx +"px",
        "top": -bigy +"px"
      })
    })
  }


  switchs("#ranking", ".ranking-content")
  switchs("#switchbottom", ".switchbottom-content")
  switchs("#switchcenter", ".switchcenter-content")
  switchs("#addressTabs", ".addressTabs-content")
  switchs("#classifyone", ".classifyone-item")
  switchs("#classifytwo", ".classifytwo-item")

  // 点击切换
  function switchs(parent, target){
    $(parent).children("p").click(function(){
      $(target).eq($(parent).find(".active").index()).hide()
      $(parent).find(".active").removeClass("active")
      $(this).addClass("active")
      $(target).eq($(this).addClass("active").index()).show()
    })
  }

  $("#live").click(function(){
    var num = $(this).parent().find("p").html().substring(1,3)
    if($(this).parent().hasClass("focus")){
      $(this).parent().removeClass("focus")
      $(this).parent().find("p").html("(" + (Number(num)-1) + ")")
      $(this).removeClass("pd-heart_solid").addClass("pd-heart_line");
    }else{
      $(this).parent().addClass("focus")
      $(this).removeClass("pd-heart_line").addClass("pd-heart_solid");
      $(this).parent().find("p").html("(" + (Number(num)+1) + ")")
    }
  })
  // 验证吗
  $('#mpanel').codeVerify({
		type : 1,
		width : '100px',
		height : '30px',
		fontSize : '16px',
		codeLength : 6,
		btnId : 'pappusRelease',
		ready : function() {
		},
		success : function() {
			alert('验证匹配！');
		},
		error : function() {
			alert('验证码不匹配！');
		}
  })
  
  // 弹出层
  $("#goconsulting").click(function(){
    $("#pappus").show()
  })
  $("#pappus .pappus-close").click(function(){
    $("#pappus").hide()
  })
  $("#pappus .pappus-box").click(function(){
    $("#pappus").hide()
  })
})